﻿/* 2011/11/30 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Cosmos.Shortcuts;

namespace PictureManager
{
    /// <summary>
    /// A theItem which is able to be added to ListView. It contains a shortcut operation and its shortcut actions.
    /// </summary>
    abstract class ShortcutActionListViewItem : ListViewItem
    {
        protected ShortcutExecutant _executant;

        protected void DisplayOperation()
        {
            SubItems[0].Text = PictureManagerShortcutManager.GetOperationDescription(
                (PictureManagerOperationCategory)_executant.Operation);
        }

        protected void DisplayArguments()
        {
            if (_executant.Arguments == null)
            {
                SubItems[1].Text = String.Empty;
                return;
            }

            string[] arguments = _executant.Arguments;
            SubItems[1].Text = String.Join(", ", arguments);
        }

        public ShortcutOperation ShortcutOperation
        {
            get { return _executant.ShortcutOperation; }
            set
            {
                _executant.ShortcutOperation = value;
                DisplayOperation();
                DisplayArguments();
            }
        }

        public PictureManagerOperationCategory Operation
        {
            get
            { return (PictureManagerOperationCategory)_executant.Operation; }
            set
            {
                _executant.Operation = (int)value;
                DisplayOperation();
            }
        }

        public string[] Arguments
        {
            get
            { return _executant.Arguments; }
            set
            {
                if (value != null)
                    _executant.Arguments = value;
                else
                    _executant.Arguments = new string[0];
                DisplayArguments();
            }
        }
        /*
        public string Argument
        {
            get
            {
                if (_executant.Arguments != null && _executant.Arguments.Length > 0)
                    return _executant.Arguments[0];
                else
                    return null;
            }
            set
            {
                if (value != null)
                {
                    if (_executant.Arguments.Length == 1)
                        _executant.Arguments[0] = value;
                    else
                        _executant.Arguments = new string[] { value };
                }
                else
                {
                    _executant.Arguments = new string[0];
                }
                DisplayArguments();
            }
        }
        */
        public string EscapeString
        {
            get { return _executant.EscapeString; }
        }
    }


    /// <summary>
    /// A theItem which is able to be added to ListView. It contains a shortcut operation and its key combinations.
    /// </summary>
    class KeyCombinationListViewItem : ShortcutActionListViewItem
    {
        protected new KeyCombinationExecutant _executant;

        public KeyCombinationListViewItem(PictureManagerOperationCategory operation, params string[] arguments)
        {
            _executant = new KeyCombinationExecutant(operation, arguments);
            base._executant = _executant;

            SubItems.Add(String.Empty);
            SubItems.Add(String.Empty);

            DisplayOperation();
            DisplayArguments();
        }

        public KeyCombinationListViewItem(string escapeString)
        {
            _executant = new KeyCombinationExecutant(escapeString);
            base._executant = _executant;

            SubItems.Add(String.Empty);
            SubItems.Add(String.Empty);

            DisplayOperation();
            DisplayArguments();
            DisplayKeyCombinations();
        }

        private void DisplayKeyCombinations()
        {
            if (_executant.ActionCount == 0)
            {
                SubItems[2].Text = String.Empty;
                return;
            }

            StringBuilder stringBuilder = new StringBuilder();
            foreach (KeyCombination aKeyCombination in _executant.KeyCombinations)
            {
                stringBuilder.Append("(");
                stringBuilder.Append(String.Concat(PictureManagerShortcutManager.GetKeyDescription(aKeyCombination.Key)));
                if (aKeyCombination.Control)
                    stringBuilder.Append(" ctrl");
                if (aKeyCombination.Alter)
                    stringBuilder.Append(" alt");
                if (aKeyCombination.Shift)
                    stringBuilder.Append(" shift");
                stringBuilder.Append(")");
            }
            SubItems[2].Text = stringBuilder.ToString();
        }

        public bool Contains(KeyCombination keyCombination)
        {
            return _executant.Contains(keyCombination);
        }

        public void Add(KeyCombination keyCombination)
        {
            _executant.Add(keyCombination);
            DisplayKeyCombinations();
        }

        public void Remove(KeyCombination keyCombination)
        {
            _executant.Remove(keyCombination);
            DisplayKeyCombinations();
        }

        public void RemoveAt(int index)
        {
            _executant.RemoveAt(index);
            DisplayKeyCombinations();
        }

        public void Clear()
        {
            _executant.Clear();
            DisplayKeyCombinations();
        }
    }

    /// <summary>
    /// A theItem which is able to be added to ListView. It contains a shortcut operation and its mouse gestures.
    /// </summary>
    class MouseGestureListViewItem : ShortcutActionListViewItem
    {
        protected new MouseGestureExtant _executant;

        public MouseGestureListViewItem(PictureManagerOperationCategory operation, params string[] arguments)
        {
            _executant = new MouseGestureExtant(operation, arguments);
            base._executant = _executant;

            SubItems.Add(String.Empty);
            SubItems.Add(String.Empty);

            DisplayOperation();
            DisplayArguments();
        }

        public MouseGestureListViewItem(string escapeString)
        {
            _executant = new MouseGestureExtant(escapeString);
            base._executant = _executant;

            SubItems.Add(String.Empty);
            SubItems.Add(String.Empty);

            DisplayOperation();
            DisplayArguments();
            DisplayMouseGesture();
        }

        private void DisplayMouseGesture()
        {
            if (_executant.ActionCount == 0)
            {
                SubItems[2].Text = String.Empty;
                return;
            }

            StringBuilder stringBuilder = new StringBuilder();
            foreach (MouseGesture aMouseGesture in _executant.MouseGestures)
            {
                stringBuilder.Append("(");
                stringBuilder.Append(String.Join(" ", aMouseGesture.GetDirectionSymbols()));
                stringBuilder.Append(")");
            }
            SubItems[2].Text = stringBuilder.ToString();
        }

        public bool Contains(MouseGesture mouseGesture)
        {
            return _executant.Contains(mouseGesture);
        }

        public void Add(MouseGesture mouseGesture)
        {
            _executant.Add(mouseGesture);
            DisplayMouseGesture();
        }

        public void Remove(MouseGesture mouseGesture)
        {
            _executant.Remove(mouseGesture);
            DisplayMouseGesture();
        }

        public void RemoveAt(int index)
        {
            _executant.RemoveAt(index);
            DisplayMouseGesture();
        }

        public void Clear()
        {
            _executant.Clear();
            DisplayMouseGesture();
        }
    }

    class ShortcutActionItemSorter : IComparer
    {
        private SortOrder _order;

        public ShortcutActionItemSorter()
            : this(SortOrder.Ascending)
        {
        }

        public ShortcutActionItemSorter(SortOrder order)
        {
            _order = order;
        }

        public int Compare(object first, object second)
        {
            ShortcutActionListViewItem firstItem = (ShortcutActionListViewItem)first;
            ShortcutActionListViewItem secondItem = (ShortcutActionListViewItem)second;
            int result = 0;
            if (firstItem.Operation > secondItem.Operation)
            {
                result = 1;
            }
            else if (firstItem.Operation < secondItem.Operation)
            {
                result = -1;
            }
            else
            {
                if (firstItem.Arguments.Length > secondItem.Arguments.Length)
                {
                    result = 1;
                }
                else if (firstItem.Arguments.Length < secondItem.Arguments.Length)
                {
                    result = -1;
                }
                else
                {
                    string[] firstArguments = firstItem.Arguments;
                    string[] secondArguments = secondItem.Arguments;
                    for (int i = 0; i < firstArguments.Length; i++)
                    {
                        result = String.Compare(firstArguments[i], secondArguments[i]);
                        if(result!=0)
                            break;
                    }
                }
            }
            if (_order == SortOrder.Descending)
                result *= -1;
            return result;
        }

        public void ReverseOrder()
        {
            if (_order == SortOrder.Ascending)
                _order = SortOrder.Descending;
            else
                _order = SortOrder.Ascending;
        }

        public SortOrder Order
        {
            get { return _order; }
        }
    }
}
